package com.feng.test.ppt;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.UUID;
import org.apache.poi.sl.usermodel.PlaceableShape;
import org.apache.poi.sl.usermodel.Shape;
import org.apache.poi.xslf.usermodel.*;

/**
 * @author HuangSuip
 */
public class NewPPTXAnalyse {

    // XSLFGraphicFrame(图表) XSLFConnectorShape（形状）XSLFGraphicFrame（组织  公式）
    //XSLFPictureShape （图片） XSLFTextBox（文本 继承autoShape） XSLFTable（表格）
    //XSLFAutoShape（标题
    public static void main(String[] args) {
        try (InputStream inputStream = new FileInputStream(new File("E:\\ppt\\x6.pptx"))) {
            XMLSlideShow slideShow = new XMLSlideShow(inputStream);
            List<XSLFSlide> slides = slideShow.getSlides();
            for (XSLFSlide slide : slides) {
                String page = "第 " + slide.getSlideNumber() + " 页";
                System.out.println(page);
                drawSlide(slide, page);
                System.out.println();
                List<XSLFShape> shapes = slide.getShapes();
                for (XSLFShape shape : shapes) {
                    System.out.println("shapeId: " + shape.getShapeId());
                    System.out.println("shapeName: " + shape.getShapeName());
                    System.out.println("shape class: " + shape.getClass());
                    outputRectangle(shape);
                    shapeAnalyse(shape);
                    System.out.println("------");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void outputRectangle(XSLFShape shape) {
        Rectangle2D rectangle2D = shape.getAnchor();
        System.out.println("MinX: " + rectangle2D.getMinX());
        System.out.println("MinY: " + rectangle2D.getMinY());
        System.out.println("Rectangle2D X: " + rectangle2D.getX());
        System.out.println("Rectangle2D Y: " + rectangle2D.getY());
        System.out.println("CenterX: " + rectangle2D.getCenterX());
        System.out.println("CenterY: " + rectangle2D.getCenterY());
        System.out.println("Max X: " + rectangle2D.getMaxX());
        System.out.println("Max Y: " + rectangle2D.getMaxY());
        System.out.println("Rectangle2D H: " + rectangle2D.getHeight());
        System.out.println("Rectangle2D W: " + rectangle2D.getWidth());


    }


    private static void shapeAnalyse(XSLFShape shape) {
        if (shape instanceof XSLFConnectorShape) {
            XSLFConnectorShape connectorShape = (XSLFConnectorShape) shape;
            connectorShapeAnalyse(connectorShape);
        }
        if (shape instanceof XSLFAutoShape) {
            XSLFAutoShape autoShape = (XSLFAutoShape) shape;
            autoShapeAnalyse(autoShape);

        }
        if (shape instanceof XSLFFreeformShape) {
            XSLFFreeformShape freeformShape = (XSLFFreeformShape) shape;
            freeformShapeAnalyse(freeformShape);
        }
        if (shape instanceof XSLFGroupShape){
            XSLFGroupShape groupShape = (XSLFGroupShape) shape;
            groupShape(groupShape);
        }
    }

    private static void groupShape(XSLFGroupShape groupShape) {
        //drawShape(groupShape, groupShape.getShapeName());
    }

    private static void freeformShapeAnalyse(XSLFFreeformShape freeformShape) {
        //drawShape(freeformShape, freeformShape.getShapeName());
    }

    private static void drawShape(Shape shape, String... name) {
        Rectangle2D rectangle2D = shape.getAnchor();
        //rectangle2D.setRect(0, 0, rectangle2D.getWidth(), rectangle2D.getHeight());
        BufferedImage image = new BufferedImage((int) rectangle2D.getWidth() + 5, (int) rectangle2D.getHeight() + 1, BufferedImage.TYPE_INT_ARGB);
        System.out.println("Image Height: " + image.getHeight());
        System.out.println("Image Width: " + image.getWidth());

        Graphics2D graphics2D = image.createGraphics();
        graphics2D.setBackground(Color.blue);
        Rectangle2D rectangle2D1 = new Rectangle2D.Double(0, 0, rectangle2D.getWidth() + 5, rectangle2D.getHeight());
/*        image = graphics2D.getDeviceConfiguration().createCompatibleImage((int) rectangle2D.getWidth(), (int) rectangle2D.getHeight() + 1, Transparency.TRANSLUCENT);
        graphics2D.dispose();
        graphics2D = image.createGraphics();*/
        //graphics2D.rotate(shape.getRotation(),rectangle2D.getX(), rectangle2D.getY());
/*        graphics2D.setPaint(Color.white);
        graphics2D.setBackground(Color.white);*/
        double rotation = 0;
        PlaceableShape ps = null;
        if (shape instanceof PlaceableShape) {
            ps = (PlaceableShape) shape;
            rotation = ps.getRotation();
            if (rotation > 0) {
                ps.setRotation(0);
            }
        }

        shape.draw(graphics2D, rectangle2D1);

        if (rotation > 0) {
            ps.setRotation(rotation);
        }
        try {
            String fileName = name.length > 0 ? name[0] : "";
            FileOutputStream out = new FileOutputStream(
                    new File("E:\\ppt\\slidepic\\" + fileName.replaceAll(":", "") + " " + UUID.randomUUID().toString().substring(0, 5) + ".png"));
            javax.imageio.ImageIO.write(image, "png", out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void autoShapeAnalyse(XSLFAutoShape autoShape) {
        System.out.println("Shape type: " + autoShape.getShapeType());
        System.out.println("Shape text: " + autoShape.getText());
        System.out.println("Rotation: " + autoShape.getRotation());
        //drawShape(autoShape, autoShape.getShapeName());
        System.out.println("Rotation: " + autoShape.getRotation());
    }

    private static void connectorShapeAnalyse(XSLFConnectorShape shape) {
        Rectangle2D anchor = shape.getAnchor();
        System.out.println("Rotation: " + shape.getRotation());
        System.out.println("Shape Type: " + shape.getShapeType());
        System.out.println("LineWidth: " + shape.getLineWidth());
        System.out.println("D_X: " + (anchor.getMaxX()- anchor.getMinX()));
        //复合类型 ()
        System.out.println("Compound: " + shape.getLineCompound());
        //线条类型 轮廓 (短划线类型)
        System.out.println("Outline: " + shape.getLineDash());
        //末端类型(线端类型)
        System.out.println("End cap style: " + shape.getLineCap());
        //线条属性 包括头 尾形状 大小
        System.out.println("Decoration: " + shape.getLineDecoration());
        //System.out.println(shape.getXmlObject());
        System.out.println("Shadow: " + shape.getShadow());
        System.out.println("FlipVertical: " + shape.getFlipVertical());
        System.out.println("FlipHorizontal: " + shape.getFlipHorizontal());
        System.out.println("LineHeadLength: " + shape.getLineHeadLength());
        System.out.println("LineTailLength: " + shape.getLineTailLength());

        System.out.println("LineHeadShape: " + shape.getLineHeadDecoration());
        System.out.println("LineTailShape: " + shape.getLineTailDecoration());
        drawShape(shape, shape.getShapeName());
        System.out.println();
    }



    private static void drawSlide(XSLFSlide slide, String... name) {
        Dimension dimension = slide.getSlideShow().getPageSize();
        BufferedImage image = new BufferedImage(
                (int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.fill(new Rectangle2D.Double(0, 0, dimension.getWidth(), dimension.getHeight()));
        slide.draw(graphics2D);
        String fileName = name.length > 0 ?  name[0] : "";
        try {
            FileOutputStream out = new FileOutputStream(
                    new File("E:\\ppt\\slidepic\\" + fileName + UUID.randomUUID().toString().substring(0,5) + ".png"));
            javax.imageio.ImageIO.write(image, "png", out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
